/**
 * @Author:Geln Yang
 * @Created:2008-7-30 $ Revision: 1.0 $ $ Author: Geln Yang $ $ Date: 2008-7-30 $
 */
package com.hgy.commonsale.user.domain;

import java.util.Date;
import java.util.Properties;
import org.geln.encode.EncodeUtil;
import org.geln.properties.PropertiesUtil;

/**
 * @Author:Geln Yang
 * @Created:2008-7-30
 */
public class User {
	static {
		Properties properties = PropertiesUtil.getProperties("user.properties");
		String str = properties.getProperty("user.login.sameIP.error.maxTime");
		if (str != null) {
			int value = Integer.parseInt(str);
			if (value > 0)
				SAME_IP_ERROR_LOGIN_TIME = value;
		}
		str = properties.getProperty("user.account.min.length");
		if (str != null) {
			int value = Integer.parseInt(str);
			if (value > 0)
				ACCOUNT_MIN_LENGTH = value;
		}
		str = properties.getProperty("user.account.max.length");
		if (str != null) {
			int value = Integer.parseInt(str);
			if (value > 0)
				ACCOUNT_MAX_LENGTH = value;
		}
		str = properties.getProperty("user.password.min.length");
		if (str != null) {
			int value = Integer.parseInt(str);
			if (value > 0)
				PASSWORD_MIN_LENGTH = value;
		}
		str = properties.getProperty("uuser.password.max.length");
		if (str != null) {
			int value = Integer.parseInt(str);
			if (value > 0)
				PASSWORD_MAX_LENGTH = value;
		}
	}

	public static final int USER_STATUS_INACTIVE = -1;

	public static final int USER_STATUS_NORMAL = 0;

	public static final int USER_STATUS_LOCKED = 1;

	public static int SAME_IP_ERROR_LOGIN_TIME = 10;

	public static int ACCOUNT_MIN_LENGTH = 4;

	public static int ACCOUNT_MAX_LENGTH = 20;

	public static int PASSWORD_MIN_LENGTH = 5;

	public static int PASSWORD_MAX_LENGTH = 40;

	public static int PASSWORD_ENCODE_LENGTH = 40;

	private Date registrationTime;

	private Date lastLoginTime;

	private String id;

	private String userName;

	private String fullName;

	private String account;

	private String password;

	private String email;

	private String image;

	private String lastLoginIP;

	private int status;

	private int level;

	private long loginTimes;

	private int failedLoginTimes;

	private Date modifiedTime;

	public boolean authorize(String pass) {
		if (pass.length() != PASSWORD_ENCODE_LENGTH) {
			pass = EncodeUtil.hash(pass);
		}
		if (password.equals(pass)) {
			return true;
		} else {
			return false;
		}
	}

	public void copyUpdateInfo(User source) {
		setEmail(source.getEmail() == null ? email : source.getEmail());
		setUserName(source.getUserName() == null ? userName : source.getUserName());
		setFullName(source.getFullName() == null ? fullName : source.getFullName());
		setImage(source.getImage() == null ? image : source.getImage());
		setPassword(source.getPassword() == null ? password : source.getPassword());
	}

	public String getLastLoginIP() {
		return lastLoginIP;
	}

	public void setLastLoginIP(String lastLoginIP) {
		this.lastLoginIP = lastLoginIP;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getFailedLoginTimes() {
		return failedLoginTimes;
	}

	public void setFailedLoginTimes(int failedLoginTimes) {
		this.failedLoginTimes = failedLoginTimes;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		if (isValidAccount(account)) {
			this.account = account;
		}
	}

	public static boolean isValidAccount(String account) {
		if (account == null) {
			return false;
		}
		account = account.trim();
		if (account.length() < ACCOUNT_MIN_LENGTH || account.length() > ACCOUNT_MAX_LENGTH) {
			return false;
		}
		return true;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		if (password == null) {
			throw new IllegalArgumentException("User's password can't be null!");
		}
		password = password.trim();
		if (password.length() < PASSWORD_MIN_LENGTH || password.length() > PASSWORD_MAX_LENGTH) {
			throw new IllegalArgumentException("The length of user's password must between " + PASSWORD_MIN_LENGTH
					+ " and " + PASSWORD_MAX_LENGTH + "!");
		}
		if (password.length() == PASSWORD_MAX_LENGTH) {
			this.password = password;
		} else {
			this.password = EncodeUtil.hash(password);
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getRegistrationTime() {
		return registrationTime;
	}

	public void setRegistrationTime(Date registrationTime) {
		this.registrationTime = registrationTime;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public long getLoginTimes() {
		return loginTimes;
	}

	public void setLoginTimes(long loginTimes) {
		this.loginTimes = loginTimes;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Date getModifiedTime() {
		return modifiedTime;
	}

	public void setModifiedTime(Date modifiedTime) {
		this.modifiedTime = modifiedTime;
	}

}
